﻿using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.ObjectModel;
using Eassets.Layered.Domain.Asset;

namespace Eassets.Data.Asset
{
    public class FixedAssetDisposalDetailsData
    {
        /// <summary>
        /// Inserts AssetDisposalDetails.
        /// </summary>
        /// <param name="assetDisposalDetails"></param>
        public void AddFixedAssetDisposalDetails(AssetDisposalDetails assetDisposalDetails)
        {
            SqlCommand command = new SqlCommand("SP_AddFixedAssetDisposalDetails"); // Name of the SP
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, assetDisposalDetails));
        }

        /// <summary>
        /// Updates AssetDisposalDetails.
        /// </summary>
        /// <param name="assetDisposalDetails"></param>
        public void ModifyFixedAssetDisposalDetails(AssetDisposalDetails assetDisposalDetails)
        {
            SqlCommand command = new SqlCommand("SP_ModifyFixedAssetDisposalDetails"); // Name of the SP
            command.Parameters.AddWithValue("@FixedAssetDisposalDetailsId", assetDisposalDetails.AssetDisposalDetailsId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, assetDisposalDetails));
        }


        /// <summary>
        /// Delete AssetDisposalDetails.
        /// </summary>
        /// <param name="fixedAssetDisposalDetailsId"></param>
        public void DeleteFixedAssetDisposalDetails(long fixedAssetDisposalDetailsId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteFixedAssetDisposalDetails"); // Name of the SP
            command.Parameters.AddWithValue("@FixedAssetDisposalDetailsId", fixedAssetDisposalDetailsId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// Get AssetDisposalDetails by Id.
        /// </summary>
        /// <param name="fixedAssetDisposalDetailsId"></param>
        /// <returns></returns>
        public AssetDisposalDetails GetByIdFixedAssetDisposalDetails(long fixedAssetDisposalDetailsId)
        {
            SqlCommand command = new SqlCommand("SP_GetFixedAssetDisposalDetailsById");
            command.Parameters.AddWithValue("@FixedAssetDisposalDetailsId", fixedAssetDisposalDetailsId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetDisposalDetails AssetDisposalDetails = new AssetDisposalDetails();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["AssetDisposalDetailsId"].ToString())))
                {
                    AssetDisposalDetails.AssetDisposalDetailsId = Convert.ToInt64(dataReader["AssetDisposalDetailsId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    AssetDisposalDetails.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    AssetDisposalDetails.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetItemSerialCode"].ToString())))
                {
                    AssetDisposalDetails.AssetItemSerialCode = Convert.ToString(dataReader["AssetItemSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DisposalDate"].ToString())))
                {
                    AssetDisposalDetails.DisposalDate = Convert.ToDateTime(dataReader["DisposalDate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RemainingLifeTime"].ToString())))
                {
                    AssetDisposalDetails.RemainingLifeTime = Convert.ToInt16(dataReader["RemainingLifeTime"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["SuggestedValue"].ToString())))
                {
                    AssetDisposalDetails.SuggestedValue = Convert.ToDecimal(dataReader["SuggestedValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Reason"].ToString())))
                {
                    AssetDisposalDetails.Reason = Convert.ToString(dataReader["Reason"]);
                }
            }
            dataReader.Close();
            return AssetDisposalDetails;
        }

        /// <summary>
        /// Get All AssetDisposalDetails.
        /// </summary>
        /// <returns></returns>
        public Collection<AssetDisposalDetails> GetAllFixedAssetDisposalDetails()
        {
            Collection<AssetDisposalDetails> fixedAssetDisposalDetailss = new Collection<AssetDisposalDetails>();
            SqlCommand command = new SqlCommand("sproc_t_l_GetAllFixedAssetDisposalDetailss");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetDisposalDetails AssetDisposalDetails;
            while (dataReader.Read())
            {
                AssetDisposalDetails = new AssetDisposalDetails();
                if (!(String.IsNullOrEmpty(dataReader["AssetDisposalDetailsId"].ToString())))
                {
                    AssetDisposalDetails.AssetDisposalDetailsId = Convert.ToInt64(dataReader["AssetDisposalDetailsId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["TransactionId"].ToString())))
                {
                    AssetDisposalDetails.TransactionId = Convert.ToInt64(dataReader["TransactionId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    AssetDisposalDetails.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetItemSerialCode"].ToString())))
                {
                    AssetDisposalDetails.AssetItemSerialCode = Convert.ToString(dataReader["AssetItemSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DisposalDate"].ToString())))
                {
                    AssetDisposalDetails.DisposalDate = Convert.ToDateTime(dataReader["DisposalDate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RemainingLifeTime"].ToString())))
                {
                    AssetDisposalDetails.RemainingLifeTime = Convert.ToInt16(dataReader["RemainingLifeTime"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["SuggestedValue"].ToString())))
                {
                    AssetDisposalDetails.SuggestedValue = Convert.ToDecimal(dataReader["SuggestedValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Reason"].ToString())))
                {
                    AssetDisposalDetails.Reason = Convert.ToString(dataReader["Reason"]);
                }

                fixedAssetDisposalDetailss.Add(AssetDisposalDetails);
            }
            dataReader.Close();
            return fixedAssetDisposalDetailss;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assetId"></param>
        /// <returns></returns>
        public Collection<AssetItemDetails> GetFixedAssetItemDetailsByAssetId(long assetId)
        {
            SqlCommand command = new SqlCommand("SP_GetAssetItemDetailsByAssetId");
            command.Parameters.AddWithValue("@AssetId", assetId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            AssetItemDetails assetItemDetails;
            Collection<AssetItemDetails> assetItemDetailCollection = new Collection<AssetItemDetails>();
            while (dataReader.Read())
            {
                assetItemDetails = new AssetItemDetails();
                if (!(String.IsNullOrEmpty(dataReader["ItemDetailsId"].ToString())))
                {
                    assetItemDetails.ItemDetailsId = Convert.ToInt64(dataReader["ItemDetailsId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    assetItemDetails.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetSerialCode"].ToString())))
                {
                    assetItemDetails.AssetSerialCode = Convert.ToString(dataReader["AssetSerialCode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetStatus"].ToString())))
                {
                    assetItemDetails.AssetStatus = Convert.ToString(dataReader["AssetStatus"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["PurchasedDate"].ToString())))
                {
                    assetItemDetails.PurchasedDate = Convert.ToDateTime(dataReader["PurchasedDate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["PurchasedValue"].ToString())))
                {
                    assetItemDetails.PurchasedValue = Convert.ToDecimal(dataReader["PurchasedValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["CurrentBookValue"].ToString())))
                {
                    assetItemDetails.CurrentBookValue = Convert.ToDecimal(dataReader["CurrentBookValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["RemainingLifeTime"].ToString())))
                {
                    assetItemDetails.RemainingLifeTime = Convert.ToInt16(dataReader["RemainingLifeTime"]);
                }
                assetItemDetailCollection.Add(assetItemDetails);
            }

            dataReader.Close();
            return assetItemDetailCollection;
        }

        public SqlCommand ConstructCommand(SqlCommand command, AssetDisposalDetails AssetDisposalDetails)
        {
            command.CommandType = CommandType.StoredProcedure;


            try
            {
                command.Parameters.AddWithValue("@TransactionId", AssetDisposalDetails.TransactionId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetId", AssetDisposalDetails.AssetId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetItemSerialCode", AssetDisposalDetails.AssetItemSerialCode);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@DisposalDate", AssetDisposalDetails.DisposalDate);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@RemainingLifeTime", AssetDisposalDetails.RemainingLifeTime);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@SuggestedValue", AssetDisposalDetails.SuggestedValue);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Reason", AssetDisposalDetails.Reason);
            }
            catch (Exception)
            { }
            return command;
        }

    }
}

